package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.TradeWarActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_TRADE_WAR_ACTIVITY_UNION_RANK, desc = "跨服商战商会积分排行榜")
public class CrossTradeWarUnionRankListCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityUnionRankListReqMsg reqMsg = CommonActivityProto.CommonActivityUnionRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
      /*  CommonActivityProto.CommonActivityUnionRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityUnionRankListRespMsg.newBuilder();
        UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = CrossDataMgr.getCrossRankActivityUnionRankMsg(activityId);
        ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        UnionActivityConditionData unionActivityConditionData = null;
        if(null != activityInfo) {
            int conditionType = TradeWarActivityMgr.CONDITION_TYPE_UNION;
            maxRank = TradeWarActivityMgr.getMaxRankByType(activityId, conditionType);
            unionActivityConditionData = TradeWarActivityRankMgr.getUnionActivityConditionData(activityInfo.getActivityId(), player.getUserId());
        }
        while (null != rankListMsg.getRankListList() && rankListMsg.getRankListCount() > maxRank){
            rankListMsg.removeRankList(rankListMsg.getRankListCount() - 1);
        }
        respMsg.setRankList(rankListMsg);
        int rank = -1;

        if(null != unionActivityConditionData) {
            respMsg.setValue(unionActivityConditionData.getValue().toString());
            if(!StringUtils.isNullOrEmpty(unionActivityConditionData.getUnionUid())){
                rank = ActivityMgr.getMyCrossUnionRank(unionActivityConditionData.getUnionUid(), rankListMsg);
            }
        }else {
            respMsg.setValue("0");
        }
        respMsg.setMyRank(rank);
        player.sendChannelPacket(Protocol.U_TRADE_WAR_ACTIVITY_UNION_RANK, respMsg);*/


//        String unionId = "";
//        String localValue = "0";
        /*UnionActivityConditionData ucd = TradeWarActivityRankMgr.getUnionActivityConditionData(activityId, player.getUserId());
        if (null != ucd) {
            unionId = ucd.getUnionUid();
            localValue = ucd.getValue().toString();
        }*/
//        CrossActivityProto.CrossUnionActivtyRankReqMsg.Builder req = CrossActivityProto.CrossUnionActivtyRankReqMsg.newBuilder();
//        req.setActivtyId(activityId);
//        req.setConditionType(0);
//        req.setLocalValue(localValue);
//        req.setUnionId(unionId);
//        player.sendPacket(CrossProtocol.C2_CROSS_GET_TRADE_WAR_ACTIVITY_UNION_RANK, req);

        YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
        builder.setIdKey(String.valueOf(eGamePlayerEventType.CrossTradeWarUnionScoreRiseRank.getValue()));
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_LIST, builder, activityId);
        return;
    }
}
